//
//  MTDBOperation.m
//  MTDBFROMJSON
//
//  Created by kingPC on 13-8-25.
//  Copyright (c) 2013年 kingPC. All rights reserved.
//

#import "MTDBOperation.h"
#import "AppDelegate.h"

@implementation MTDBOperation

-(BOOL)UpDataDB:(NSDictionary*)dFromJson createTableSQL:(NSString*)CSQL tableName:(NSString*)TBName{
    db=[[DatabaseOperation alloc]initWithDbName:database_name];
    if (![db createTable:CSQL]) {
        return NO;
    }
    
    NSString *sql=@"select id from `%@`";
    NSString *stringSql=[NSString stringWithFormat:sql,TBName];
    NSArray *ids=[db querryTable:stringSql];
    
    if (![self insertData:dFromJson tableName:TBName ids:ids]) {
        return NO;
    }
    return YES;
}

-(BOOL)insertData:(NSDictionary*)dFromJson tableName:(NSString*)TBName ids:(NSArray*)ids{
    NSArray *keys=[dFromJson allKeys];
//    NSString *idkey=[keys objectAtIndex:0];
    NSString *idkey=@"id";
    NSDictionary *dir=[NSDictionary dictionaryWithObjectsAndKeys:[dFromJson objectForKey:idkey],idkey, nil];
    
    if (ids) {
        if ([ids containsObject:dir]||[self selectData:dir tableName:TBName]) {
            return [self updataData:dFromJson tableName:TBName key:dir];
        }
    }
    
    NSString *sql=@"insert into `%@` (%@) values (%@)";
    NSString *sqlKeys=@"";
    NSString *sqlValues=@"";
    for (NSString *key in keys) {
        id Value=[dFromJson objectForKey:key];
        sqlKeys=[sqlKeys stringByAppendingFormat:@"`%@`",key];
        if ([Value isKindOfClass:[NSString class]]) {
            sqlValues=[sqlValues stringByAppendingFormat:@"'%@'",Value];
        }else{
            sqlValues=[sqlValues stringByAppendingFormat:@"%@",Value];
        }
        if (![key isEqualToString:[keys lastObject]]) {
            sqlKeys=[sqlKeys stringByAppendingString:@","];
            sqlValues=[sqlValues stringByAppendingString:@","];
        }
    }
    NSString *stringSql=[NSString stringWithFormat:sql,TBName,sqlKeys,sqlValues];
    
    if(![db InsertTable:stringSql]){
        return NO;
    }
    return YES;
}

-(NSArray*)selectData:(NSDictionary*)dFromJson tableName:(NSString*)TBName{
    NSString *sqlKeys=[[dFromJson allKeys]lastObject];
    NSString *sqlValues=[dFromJson valueForKey:sqlKeys];
    NSString *sql=@"select * from `%@` where `%@`= %@";
    NSString *stringSql=[NSString stringWithFormat:sql,TBName,sqlKeys,sqlValues];
    NSArray *a=[db querryTable:stringSql];
    if(!a){
        return NO;
    }
    return a;
}

-(BOOL)updataData:(NSDictionary*)dFromJson tableName:(NSString*)TBName key:(NSDictionary*)keyDir{
    NSString *sqlKey=[[keyDir allKeys]lastObject];
    NSString *sqlValue=[keyDir valueForKey:sqlKey];
    
    NSMutableDictionary *tempDir=[NSMutableDictionary dictionaryWithDictionary:dFromJson];
    [tempDir removeObjectForKey:sqlKey];
    NSArray *keys=[tempDir allKeys];
    
    NSString *sql=@"UPDATE `%@` SET %@ WHERE `%@` = %@";
    NSString *tempSQL=@"";
    for (NSString *key in keys) {
        id Value=[dFromJson objectForKey:key];
        if (Value!=Nil&&![Value isKindOfClass:[NSNull class]]) {
            
            if ([Value isKindOfClass:[NSString class]]) {
                tempSQL=[tempSQL stringByAppendingString:[NSString stringWithFormat:@"`%@`='%@'",key,Value]];
            }else{
                tempSQL=[tempSQL stringByAppendingString:[NSString stringWithFormat:@"`%@`=%@",key,Value]];
            }
            
            
        }
        if (![key isEqualToString:[keys lastObject]]) {
            tempSQL=[tempSQL stringByAppendingString:@","];
        }
    }
    NSString *stringSql=[NSString stringWithFormat:sql,TBName,tempSQL,sqlKey,sqlValue];
    
    if(![db UpdataTable:stringSql]){
        return NO;
    }
    return YES;
}

@end
